

import ij.IJ;
import ij.ImagePlus;
import ij.gui.NewImage;
import ij.plugin.filter.PlugInFilter;
import ij.process.Blitter;
import ij.process.ImageProcessor;

import pluginUtils.Complexe; 

import java.util.Random; 

/**
 * 
 */

/**
 * @author Francesco
 *
 */
public class ParalTransform_ implements PlugInFilter {
	private int imgCols; 
	private int imgRows;
	
	/* (non-Javadoc)
	 * @see ij.plugin.filter.PlugInFilter#run(ij.process.ImageProcessor)
	 */
	public void run(ImageProcessor ip) {
		 //get width, height and the region of interest
			int w = ip.getWidth();     
			int h = ip.getHeight();  
			this.imgCols=w;
			this.imgRows=h;
			
			// Size of the copy
			int wArrivee=2200; 
			int hArrivee=2200; 
		

			// create a new image with the same size
			ImagePlus inverted = NewImage.createRGBImage ("New image", wArrivee, hArrivee, 1, NewImage.FILL_BLACK);
			ImageProcessor inv_ip = inverted.getProcessor();
			//inv_ip.copyBits(ip,0,0,Blitter.COPY);

				
			int[] basepixels = (int[]) ip.getPixels();
			int[] pixels=new int[wArrivee*hArrivee];
			//A
			double x1=1485.0/wArrivee; 
			double y1=999.0/hArrivee;
			// B
			double x4=1995.0/wArrivee; 
			double y4=699.0/hArrivee;
			//C
			double x3=1713.0/wArrivee; 
			double y3=1077.0/hArrivee;
			//D
			double x2=969.0/wArrivee; 
			double y2=1500.0/hArrivee; 
			
			
			
			    for(int xrow=0;xrow<imgRows;xrow++){
			    	for(int ycol=0;ycol<imgCols;ycol++){
			    		double xdepart=xrow/(this.imgRows+0.0); 
			    		double ydepart=ycol/(this.imgCols+0.0); 
			    		double Xarrivee=(1-ydepart)*((1-xdepart)*x1+xdepart*x2)+ydepart*((1-xdepart)*x4+xdepart*x3);
			    		double YArrivee=(1-ydepart)*((1-xdepart)*y1+xdepart*y2)+ydepart*((1-xdepart)*y4+xdepart*y3);
			    		//int newX=(int)(Xarrivee*wArrivee);
			    		int newX=(int)(Xarrivee*wArrivee)*wArrivee;
			    		int newY=(int)(YArrivee*hArrivee);
			    		//int valeur=(int)((Xarrivee*wArrivee*wArrivee)+(YArrivee*hArrivee)); 
			    		if(newX+newY<wArrivee*hArrivee)
			    			//pixels[newX*wArrivee+newY]=basepixels[xrow*w+ycol]; 
			    		//pixels[valeur]=basepixels[xrow*w+ycol];
			    			pixels[newX+newY]=basepixels[xrow*w+ycol];	
			    	}// for ycol
			    	
			   
			    }
			 inv_ip.setPixels(pixels); 
			 inverted.show();
			 inverted.updateAndDraw();
	
			    	
}
	
	
			
	

	/* (non-Javadoc)
	 * @see ij.plugin.filter.PlugInFilter#setup(java.lang.String, ij.ImagePlus)
	 */
	
	public int setup(String arg, ImagePlus imp) {
		if (arg.equals("about"))
			{showAbout(); return DONE;}
	            return DOES_RGB+NO_CHANGES;

	}
	
	void showAbout() {
		IJ.showMessage("Droste effect",
			"Escher-like droste effect"
		); 

	}
}